﻿<Button @onclick="SaveTable">SAVE</Button>
<Button @onclick="LoadTable">LOAD</Button>
<Button @onclick="SaveTableWithJson">SAVE JSON</Button>
<Button @onclick="LoadTableWithJson">LOAD JSON</Button>
<Button @onclick="ResetTable">RESET</Button>
<Button @onclick="GetAllFilteredRows">Get All Filtered Rows</Button>
<br />
<Table DataSource="data" OnChange="OnChange" TItem="Data" @ref="Table" Size="TableSize.Small">
    <PropertyColumn 
            Property="c=>c.Number"
            Sortable
            Filterable />

    <PropertyColumn 
            Property="c=>c.Name"
            SorterCompare="@((a,b)=> string.Compare(a,b))"
            SortDirections="new[] { SortDirection.Descending }"
            Filterable />

    <PropertyColumn
            Property="c=>c.Address"
            SorterCompare="@((a,b)=> string.Compare(a,b))"
            SortDirections="new[] { SortDirection.Descending, SortDirection.Ascending }"
            Filterable />

    <PropertyColumn
            Property="c=>c.BirthDate"
            Format="yyyy/MM/dd"
            Sortable
            Filterable />

    <PropertyColumn
            Property="c=>c.IsActive"
            Sortable
            Filterable />

    <PropertyColumn
            Property="c=>c.Gender"
            Sortable
            Filterable />

    <PropertyColumn
            Property="c=>c.Hobbies"
            Sortable
            Filterable />
</Table>

@using AntDesign.TableModels;
@using System.Text.Json;
@using System;
@using System.Text.Json.Serialization;

@code {

    class Data
    {
        public Data(int number, string name, string address, DateTime birthDate, bool isActive, Gender gender, Hobbies? hobbies)
        {
            Number = number;
            Id = Guid.NewGuid();
            Name = name;
            Address = address;
            BirthDate = birthDate;
            IsActive = isActive;
            Gender = gender;
            Hobbies = hobbies;
        }
        public int Number { get; set; }
        public Guid Id { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public DateTime BirthDate { get; set; }
        public bool IsActive { get; set; }
        public Gender Gender { get; set; }
        public Hobbies? Hobbies { get; set; }
    }

    enum Gender
    {
        Male,
        Female
    }


    [Flags]
    enum Hobbies
    {
        Reading = 1,
        Sport = 2,
        Cooking = 4
    }

    Table<Data> Table;
    QueryModel SavedQueryModel;
    string SavedQueryModelJson;

    void OnChange(QueryModel<Data> query)
    {
        Console.WriteLine(JsonSerializer.Serialize(query));
    }

    void ResetTable()
    {
        Table.ResetData();
    }

    void SaveTable()
    {
        SavedQueryModel = Table.GetQueryModel();
    }

    void LoadTable()
    {
        if (SavedQueryModel is not null)
            Table.ReloadData(SavedQueryModel);
    }

    void SaveTableWithJson()
    {
        SavedQueryModelJson = JsonSerializer.Serialize(Table.GetQueryModel());
    }

    void GetAllFilteredRows()
    {
        var queryModel = Table.GetQueryModel() as QueryModel<Data>;

        var list = queryModel.ExecuteQuery(Table.DataSource.AsQueryable());
    }

    void LoadTableWithJson()
    {
        if (SavedQueryModelJson is not null)
        {
            var settings = new JsonSerializerOptions();
            settings.Converters.Add(new SortModelConverter());
            settings.Converters.Add(new FilterModelConverter());
            QueryModel<Data> queryModel = JsonSerializer.Deserialize<QueryModel<Data>>(SavedQueryModelJson, settings);

            Table.ReloadData(queryModel);
        }
    }

    public class FilterModelConverter : JsonConverter<ITableFilterModel>
    {
        public override ITableFilterModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return JsonSerializer.Deserialize<FilterModel<string>>(ref reader, options);
        }

        public override void Write(Utf8JsonWriter writer, ITableFilterModel value, JsonSerializerOptions options)
        {
        }
    }

    public class SortModelConverter : JsonConverter<ITableSortModel>
    {
        public override ITableSortModel Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
        {
            return JsonSerializer.Deserialize<SortModel<string>>(ref reader, options);
        }

        public override void Write(Utf8JsonWriter writer, ITableSortModel value, JsonSerializerOptions options)
        {
        }
    }

    Data[] data =
    {
            new(1, "John Sunny","New York No. 1 Lake Park",new DateTime(1980,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(2, "Sara Green","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female, Hobbies.Sport),
            new(3, "White Miss", "Sidney No. 1 Lake Park",new DateTime(1977,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(4, "Sunny Red","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
            new(5, "John Brown","New York No. 1 Lake Park",new DateTime(1980,1,5),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(6, "Sara Green","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(7, "Miss Black", "Sidney No. 1 Lake Park",new DateTime(1977,1,5),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(8, "Jim Red","London No. 2 Lake Park",new DateTime(1987,1,1),false,Gender.Male,null),
            new(9, "White Sunny","New York No. 1 Lake Park",new DateTime(1981,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(10, "Sara Green","London No. 1 Lake Park",new DateTime(1983,12,8),true,Gender.Female,Hobbies.Sport),
            new(11, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1977,11,9),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(12, "Jim Red","London No. 2 Lake Park",new DateTime(1985,1,10),false,Gender.Male,null),
            new(13, "Sunny Brown","New York No. 1 Lake Park",new DateTime(1980,12,12),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(14, "Sara Sunny","London No. 1 Lake Park",new DateTime(1983,2,1),true,Gender.Female,Hobbies.Reading),
            new(15, "Joe White", "Sidney No. 1 Lake Park",new DateTime(1977,8,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(16, "Jim Miss","London No. 2 Lake Park",new DateTime(1985,1,7),false,Gender.Male,null),
            new(17, "John Brown","New York No. 1 Lake Park",new DateTime(1980,9,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(18, "Sara Green","London No. 1 Lake Park",new DateTime(1983,4,4),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(19, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1977,3,4),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(20, "Jim Red","London No. 2 Lake Park",new DateTime(1985,8,8),false,Gender.Male,null),
            new(21, "John Brown","New York No. 1 Lake Park",new DateTime(1980,7,7),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(22, "White Green","London No. 1 Lake Park",new DateTime(1993,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(23, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1997,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(24, "Jim Red","London No. 2 Lake Park",new DateTime(1995,1,1),false,Gender.Male,null),
            new(25, "Sara Green","London No. 1 Lake Park",new DateTime(1984,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(26, "Joe Anna", "Sidney No. 1 Lake Park",new DateTime(1975,11,1),true,Gender.Male,Hobbies.Sport),
            new(27, "Jim Red","London No. 2 Lake Park",new DateTime(1986,1,1),false,Gender.Male,null),
            new(28, "John White","New York No. 1 Lake Park",new DateTime(1987,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(29, "Anna Green","London No. 1 Lake Park",new DateTime(1988,12,1),true,Gender.Female, Hobbies.Sport),
            new(30, "Sunny Black", "Sidney No. 1 Lake Park",new DateTime(1979,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(31, "Jim Red","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
            new(32, "Miss Brown","New York No. 1 Lake Park",new DateTime(1989,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(33, "Sara Green","London No. 1 Lake Park",new DateTime(1989,12,1),true,Gender.Female,Hobbies.Reading ),
            new(34, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1979,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(35, "White Red","London No. 2 Lake Park",new DateTime(1989,1,1),false,Gender.Male,null),
            new(36, "John Anna","New York No. 1 Lake Park",new DateTime(1980,1,1),true,Gender.Male,Hobbies.Cooking),
            new(37, "Anna Sunny","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(38, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1977,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(39, "Jim Red","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
            new(40, "John Brown","New York No. 1 Lake Park",new DateTime(1980,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(41, "Sara Green","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(42, "Anna White", "Sidney No. 1 Lake Park",new DateTime(1977,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(43, "Jim Red","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
            new(44, "John Miss","New York No. 1 Lake Park",new DateTime(1980,1,1),true,Gender.Male, Hobbies.Reading),
            new(45, "Miss Green","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female,Hobbies.Reading | Hobbies.Sport),
            new(46, "Joe Black", "Sidney No. 1 Lake Park",new DateTime(1977,11,1),true,Gender.Male,Hobbies.Cooking ),
            new(47, "Jim Sunny","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
            new(48, "White Anna","New York No. 1 Lake Park",new DateTime(1980,1,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Reading),
            new(49, "Sara Green","London No. 1 Lake Park",new DateTime(1983,12,1),true,Gender.Female, Hobbies.Sport),
            new(50, "Joe Anna", "Sidney No. 1 Lake Park",new DateTime(1977,11,1),true,Gender.Male,Hobbies.Cooking | Hobbies.Sport),
            new(51, "Miss Red","London No. 2 Lake Park",new DateTime(1985,1,1),false,Gender.Male,null),
        };
}


